package chapter_4.demo1;

import chapter_4.Dish;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: hero生仔
 */
public class Solution {
    static List<Dish> menu = Arrays.asList(
            new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT),
            new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("french fries", true, 530, Dish.Type.OTHER),
            new Dish("rice", true, 350, Dish.Type.OTHER),
            new Dish("season fruit", true, 120, Dish.Type.OTHER),
            new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 300, Dish.Type.FISH),
            new Dish("salmon", false, 450, Dish.Type.FISH));

    public static void main(String[] args) {
        extracted01();

        extracted02();


    }

    private static void extracted02() {
        List<String> title = Arrays.asList("Java8", "In", "Action");
        Stream<String> s = title.stream();
        s.forEach(System.out::println);
        // 流只能被遍历一次，以下代码抛出一个异常，说明流已经被消费了
        s.forEach(System.out::println);
    }

    private static void extracted01() {
        List<String> list = menu.stream()
                .filter(dish -> dish.getCalories() > 530)
                .map(Dish::getName)
                .limit(3)
                .collect(Collectors.toList());

        System.out.println(list);
    }
}
